package br.com.eduquiz.session;

import br.com.eduquiz.entity.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

@Name("questionarioHome")
public class QuestionarioHome extends EntityHome<Questionario> {
	
	private static final long serialVersionUID = 3910601974198719970L;
	
	@In(create = true)
	private CategoriaHome categoriaHome;
	
	@In(create = true)
	private UsuarioHome usuarioHome;
	
	@Override
	public String persist() {
		getInstance().setDataCadastro(Calendar.getInstance());
		getInstance().setCliques(Long.valueOf(0));
		return super.persist();
	}

	public void setQuestionarioId(Long id) {
		setId(id);
	}

	public Long getQuestionarioId() {
		return (Long) getId();
	}

	@Override
	protected Questionario createInstance() {
		Questionario questionario = new Questionario();
		return questionario;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		Categoria categoria = categoriaHome.getDefinedInstance();
		if (categoria != null) {
			getInstance().setCategoria(categoria);
		}
		Usuario usuario= usuarioHome.getDefinedInstance();
		if (usuario != null) {
			getInstance().setUsuario(usuario);
		}
	}

	public boolean isWired() {
		return true;
	}

	public Questionario getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

	public List<Questao> getQuestao() {
		return getInstance().getQuestao() == null ? null : new ArrayList<Questao>(
				getInstance().getQuestao());
	}
	
}
